<template>
  <el-container class="home-container">
    <!-- 头部区域 -->
    <el-header>
      <div>
        <!-- <img src="" alt=""> -->
        <span>电商后台管理系统</span>
      </div>
      <el-button type='info' @click="logout">退出登录</el-button>
    </el-header>
    <!-- 主体区域 -->
    <el-container>
      <!-- 侧边栏菜单区域 -->
      <el-aside :width="isCollapse ? '64px' : '200px'">
        <div class="toggle-button" @click="toggleCollapse">|||</div>
        <el-menu
          :default-active="this.$route.path"
          background-color="#333744"
          text-color="#fff"
          active-text-color="#409bFF"
          unique-opened
          :collapse="isCollapse"
          :collapse-transition="false"
          router
          >
            <!-- 一级菜单 -->
            <el-submenu
              :index="'/' + item.path"
              v-for="item in menuList"
              :key="item.id"
              class="nav-item"
              >
              <!-- 一级菜单模板区域 -->
              <template slot="title">
                <!-- 图标 -->
                <i :class="iconsObj[item.id]"></i>
                <!-- 文本 -->
                <span>{{ item.authName }}</span>
              </template>
              <!-- 二级菜单 -->
              <el-menu-item
                :index="'/' + subItem.path"
                v-for="subItem in item.children"
                :key="subItem.id"
                class="sub-item-item"
                >
                <template slot="title">
                  <!-- 图标 -->
                  <i class="el-icon-menu"></i>
                  <!-- 文本 -->
                  <span>{{ subItem.authName }}</span>
                </template>
              </el-menu-item>
            </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 右侧内容主体 -->
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
import { mapState } from 'vuex'

export default {
  name: 'home',
  data () {
    return {
      // 图标对象
      iconsObj: {
        125: 'iconfont icon-users',
        103: 'iconfont icon-tijikongjian',
        101: 'iconfont icon-shangpin',
        102: 'iconfont icon-danju',
        145: 'iconfont icon-baobiao'
      },
      // 菜单是否折叠
      isCollapse: true
    }
  },
  created() {
    // 获取菜单列表
    this.$store.dispatch('getMenuList')
  },
  methods: {
    // 退出登录
    logout() {
      this.$confirm('确定要退出吗？', '确认信息', {
        distinguishCancelAndClose: true,
        confirmButtonText: '确定',
        cancelButtonText: '取消'
      })
        .then(() => {
          this.$store.dispatch('logout', this.userInfo)
          this.$router.replace('/login')
        })
        .catch(() => {
          this.$message.info('已取消删除')
        })
    },
    // 点击按钮，切换菜单的展开与折叠
    toggleCollapse() {
      this.isCollapse = !this.isCollapse
    }
  },
  computed: {
    ...mapState(['menuList'])
  }
}
</script>

<style lang="less" scoped>
.home-container {
  height: 100%;
}

.el-header {
  background-color: #373d41;
  display: flex;
  justify-content: space-between;
  align-items: center;
  color: #fff;
  > div {
    display: flex;
    align-items: center;
    > span {
      font-size: 20px;
    }
  }
}

.el-aside {
  user-select: none;
  background-color: #333744;
  .el-menu {
    border-right: none;
  }
}

.iconfont {
  margin-right: 10px;
}

.el-main {
  background-color: #eaedf1;
}

.toggle-button {
  background-color: #4A5064;
  text-align: center;
  font-size: 10px;
  color: #fff;
  letter-spacing: 0.2em;
  line-height: 24px;
  cursor: pointer;
}
</style>
